package com.biomatch.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

public class DateUtils {
    public static String parseDate(Date date, String format){
        SimpleDateFormat dFormat = new SimpleDateFormat(format);
        dFormat.setLenient(false);
        String dateStr = dFormat.format(date);
        return dateStr;
    }
    public static String getDateString(Date date, String pattern) {
        if (pattern == null || pattern.trim().equals(""))
            pattern = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        if (date == null) {
            return null;
        } else {
            return sdf.format(date);
        }
    }
    public static Map<String,Object> getPerDaysByStartAndEndDate(Date startDate, Date endDate, String dateFormat, int type, int during) {
        Map<String,Object> map = new LinkedHashMap<>();
        DateFormat format = new SimpleDateFormat(dateFormat);
        try {
            Date sDate = startDate;
            Date eDate = endDate;
            long start = sDate.getTime();
            long end = eDate.getTime();
            if (start > end) {
                return null;
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sDate);
            while (end >= start) {
                map.put(format.format(calendar.getTime()),0);
                calendar.add(type, during);
                start = calendar.getTimeInMillis();
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    // 获得某天最大时间 2017-10-15 23:59:59
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        ;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

    }

    // 获得某天最小时间 2017-10-15 00:00:00
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    //获取当月的第一天时间
    public static Date getFirstDay(Date date){
//        Date date = null;
//        String day_first = null;
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        date = format.parse(datadate);
        //创建日历
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
//        day_first = format.format(calendar.getTime());
//        return day_first;
    }

    //获取当月的最后一天时间
    public static Date getLastDay(Date date){
//        Date date = null;
//        String day_last = null;
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        date = format.parse(datadate);

        //创建日历
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);//加一个月
        calendar.set(Calendar.DATE, 1);//设置为该月第一天
        calendar.add(Calendar.DATE, -1); //再减一天即为上个月最后一天
        return calendar.getTime();
//        day_last = format.format(calendar.getTime());
//        return day_last;
    }

/*    public static void main(String[] args) {
        Date now = new Date();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(now);
        rightNow.add(Calendar.DAY_OF_YEAR,-31);//日期加10天
        Date dt1=rightNow.getTime();
        List<String> strings = DateUtils.getPerDaysByStartAndEndDate(dt1,now,"MM月dd日",Calendar.MONTH,-1);
        for(String s:strings){
            System.out.println(s);
        }
    }*/
}
